package com.zabra.zl.ui.inputpwd;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseActivity;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.myview.pwdview.OnPasswordInputFinish;
import com.zabra.zl.myview.pwdview.PasswordView;
import com.zabra.zl.ui.deposit.DepositActivity;
import com.zabra.zl.ui.inputpwd.contract.InputAginPwdContract;
import com.zabra.zl.ui.inputpwd.presenter.InputAginPwdPresenter;
import com.zabra.zl.ui.mybank.MyBankActivity;
import com.zabra.zl.ui.personal_center.PersonalActivity;
import com.zabra.zl.ui.success.UnbundleBankSuccessActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InputAginPwdActivity extends BaseMvpActivity<InputAginPwdPresenter> implements InputAginPwdContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.pwd_view)
    PasswordView passview;
    private String resultPwd, firstPwd, strTel, strMessage, strCardId;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_input_agin_pwd;
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {
        mPresenter = new InputAginPwdPresenter();
        mPresenter.attachView(this);
        strTel = SharedPreferencesUtil.getInstance(this).getSP("tel");
        Bundle extras = getIntent().getExtras();
        strCardId = extras.getString("cardid");
        firstPwd = extras.getString("pwd");
        if (!TextUtils.isEmpty(strCardId)) {
            //从解绑界面过来
            passview.setHintText("请输入交易密码");
            passview.setOnFinishInput(new OnPasswordInputFinish() {
                @Override
                public void inputFinish() {
                    resultPwd = passview.getStrPassword();
                    keyList.add("cardId");
                    keyList.add("debtorId");
                    keyList.add("tranPwd");
                    valueList.add(strCardId);
                    valueList.add(strUserid);
                    valueList.add(resultPwd);
                    mPresenter.getUnBindData(MaptoJsonUtils.Simple(keyList,valueList));
                    keyList.clear();
                    valueList.clear();
                }
            });
        } else {
            tvTitle.setText("输入交易密码");
            passview.setHintText("请再次填写确认");
            passview.setOnFinishInput(new OnPasswordInputFinish() {
                @Override
                public void inputFinish() {
                    resultPwd = passview.getStrPassword();
                    if (resultPwd.equals(firstPwd)) {
                        keyList.add("phone");
                        keyList.add("tranPwd");
                        valueList.add(strTel);
                        valueList.add(resultPwd);
                        mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                        keyList.clear();
                        valueList.clear();
                    } else {
                        ToastUtil.showToast(InputAginPwdActivity.this, "两次密码不一致，请确认后输入");
                    }
                }
            });
        }

    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(this).putSP("token", token);
            if (strMessage.equals("MyBank") || strMessage.equals("Person")) {
                //跳转到银行卡界面
                toClass_EmptyTop(this, PersonalActivity.class);
            }
            if (strMessage.equals("Deposit")) {
                //跳转到提现界面
                toClass_EmptyTop(this, DepositActivity.class);
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        //解绑银行卡
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            toClass_EmptyTop(this, MyBankActivity.class);
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onReceiveMsg(EventMessage message) {
        strMessage = message.getMessage();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
